package com.alon.spring01.proxy.jdk.service;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @program: spring01
 * @description: 代理工厂
 * @author: yulongguang
 * @create: 2019-08-19 16:57
 **/
public class ProxyFactory {

    /**目标对象**/
    private Object target;

    public ProxyFactory(Object target) {
        this.target = target;
    }
    /**为目标对象生成代理对象**/
    public Object getProxyInstance(){

        return Proxy.newProxyInstance(target.getClass().getClassLoader(), new Class[]{target.getClass()}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("开启事务");
                Object invoke = method.invoke(proxy, args);
                System.out.println("提交事务");
                return invoke;
            }
        });
    }
}
